package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.BlogType;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：粮库中的二级分类
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/1 16:26
 * 修改备注：
 */
public class LiangkuAssort2Adapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<BlogType> types;
    private OnItemClickListener itemClickListener;

    public LiangkuAssort2Adapter(Context context, ArrayList<BlogType> types, OnItemClickListener itemClickListener) {
        super(context);
        this.mContext = context;
        this.types = types;
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return types.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_liangku_assort_2, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_assort)
        ImageView ivAssort;
        @Bind(R.id.tv_assort)
        TextView tvAssort;

        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.onClickListener(position);
                }
            });
        }

        public void setDatas(int position) {
            this.position = position;
            BlogType type = types.get(position);
            tvAssort.setText(type.getName());
            ImageUtils.loadAssort(mContext, type.getImgurl(), ivAssort);
        }
    }
}
